package com.demospringboot.system.controller;

import com.demospringboot.common.vo.Result;
import com.demospringboot.system.entity.RepairRequest;
import com.demospringboot.system.entity.RepairStatus;
import com.demospringboot.system.entity.Role;
import com.demospringboot.system.entity.User;
import com.demospringboot.system.service.IRepairRequestService;
import com.demospringboot.system.service.IUserService;
import com.demospringboot.utils.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/repair")
public class RepairRequestController {
    @Autowired
    private IRepairRequestService repairRequestService;
    @Autowired
    private IUserService userService;

    // 学生/老师提交报修
    @PostMapping("/submit")
    public Result<?> submitRepair(@RequestBody RepairRequest repairRequest, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (token == null || token.trim().isEmpty()) {
            return Result.fail("未登录或token缺失");
        }
        String username = JwtUtil.getUsername(token);
        User user = userService.lambdaQuery().eq(User::getUsername, username).one();
        if (user == null || (user.getRole() != Role.STUDENT && user.getRole() != Role.TEACHER)) {
            return Result.fail("无权限");
        }
        repairRequest.setRequestUserId(user.getId());
        repairRequest.setStatus(RepairStatus.PENDING);
        repairRequestService.save(repairRequest);
        return Result.success("报修提交成功");
    }

    // 学生/老师查询自己的报修
    @GetMapping("/my")
    public Result<List<RepairRequest>> myRepairs(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (token == null || token.trim().isEmpty()) {
            return Result.fail("未登录或token缺失");
        }
        String username = JwtUtil.getUsername(token);
        User user = userService.lambdaQuery().eq(User::getUsername, username).one();
        if (user == null) {
            return Result.fail("无权限");
        }
        List<RepairRequest> list = repairRequestService.lambdaQuery().eq(RepairRequest::getRequestUserId, user.getId()).list();
        return Result.success(list);
    }

    // 后勤人员查询分配给自己的报修
    @GetMapping("/assigned")
    public Result<List<RepairRequest>> assignedRepairs(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (token == null || token.trim().isEmpty()) {
            return Result.fail("未登录或token缺失");
        }
        String username = JwtUtil.getUsername(token);
        User user = userService.lambdaQuery().eq(User::getUsername, username).one();
        if (user == null || user.getRole() != Role.LOGISTICS_STAFF) {
            return Result.fail("无权限");
        }
        List<RepairRequest> list = repairRequestService.lambdaQuery().eq(RepairRequest::getHandlerUserId, user.getId()).list();
        return Result.success(list);
    }

    // 管理员查询所有报修
    @GetMapping("/all")
    public Result<List<RepairRequest>> allRepairs(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (token == null || token.trim().isEmpty()) {
            return Result.fail("未登录或token缺失");
        }
        String username = JwtUtil.getUsername(token);
        User user = userService.lambdaQuery().eq(User::getUsername, username).one();
        if (user == null || user.getRole() != Role.ADMIN) {
            return Result.fail("无权限");
        }
        List<RepairRequest> list = repairRequestService.list();
        return Result.success(list);
    }

    // 管理员删除报修
    @DeleteMapping("/{id}")
    public Result<?> deleteRepair(@PathVariable Long id, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (token == null || token.trim().isEmpty()) {
            return Result.fail("未登录或token缺失");
        }
        String username = JwtUtil.getUsername(token);
        User user = userService.lambdaQuery().eq(User::getUsername, username).one();
        if (user == null || user.getRole() != Role.ADMIN) {
            return Result.fail("无权限");
        }
        repairRequestService.removeById(id);
        return Result.success("删除成功");
    }

    // 管理员/后勤人员修改报修状态、分配处理人
    @PutMapping("/update")
    public Result<?> updateRepair(@RequestBody RepairRequest repairRequest, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (token == null || token.trim().isEmpty()) {
            return Result.fail("未登录或token缺失");
        }
        String username = JwtUtil.getUsername(token);
        User user = userService.lambdaQuery().eq(User::getUsername, username).one();
        if (user == null || (user.getRole() != Role.ADMIN && user.getRole() != Role.LOGISTICS_STAFF)) {
            return Result.fail("无权限");
        }
        repairRequest.setUpdateTime(LocalDateTime.now());
        repairRequestService.updateById(repairRequest);
        return Result.success("更新成功");
    }
} 